{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example Seldon Core Deployments using Helm\n",
    "<img src=\"images/deploy-graph.png\" alt=\"predictor with canary\" title=\"ml graph\"/>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prerequisites\n",
    "You will need\n",
    " - [Git clone of Seldon Core](https://github.com/SeldonIO/seldon-core)\n",
    " - A running Kubernetes cluster with kubectl authenticated\n",
    " - [python grpc tools](https://grpc.io/docs/quickstart/python.html)\n",
    " - [Helm client](https://helm.sh/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Creating a Kubernetes Cluster\n",
    "\n",
    "Follow the [Kubernetes documentation to create a cluster](https://kubernetes.io/docs/setup/).\n",
    "\n",
    "Once created ensure ```kubectl``` is authenticated against the running cluster."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "namespace/seldon created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create namespace seldon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Context \"minikube\" modified.\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl config set-context $(kubectl config current-context) --namespace=seldon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "clusterrolebinding.rbac.authorization.k8s.io/kube-system-cluster-admin created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create clusterrolebinding kube-system-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:default"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Install Helm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "serviceaccount/tiller created\n",
      "clusterrolebinding.rbac.authorization.k8s.io/tiller created\n",
      "$HELM_HOME has been configured at /home/clive/.helm.\n",
      "\n",
      "Tiller (the Helm server-side component) has been installed into your Kubernetes Cluster.\n",
      "\n",
      "Please note: by default, Tiller is deployed with an insecure 'allow unauthenticated users' policy.\n",
      "To prevent this, run `helm init` with the --tiller-tls-verify flag.\n",
      "For more information on securing your installation see: https://docs.helm.sh/using_helm/#securing-your-helm-installation\n",
      "Happy Helming!\n"
     ]
    }
   ],
   "source": [
    "!kubectl -n kube-system create sa tiller\n",
    "!kubectl create clusterrolebinding tiller --clusterrole cluster-admin --serviceaccount=kube-system:tiller\n",
    "!helm init --service-account tiller"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Waiting for deployment \"tiller-deploy\" rollout to finish: 0 of 1 updated replicas are available...\n",
      "deployment \"tiller-deploy\" successfully rolled out\n"
     ]
    }
   ],
   "source": [
    "!kubectl rollout status deploy/tiller-deploy -n kube-system"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Start seldon-core"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME:   seldon-core\n",
      "LAST DEPLOYED: Thu May  9 10:34:26 2019\n",
      "NAMESPACE: seldon-system\n",
      "STATUS: DEPLOYED\n",
      "\n",
      "RESOURCES:\n",
      "==> v1/Secret\n",
      "NAME                                   TYPE    DATA  AGE\n",
      "seldon-operator-webhook-server-secret  Opaque  0     0s\n",
      "\n",
      "==> v1beta1/CustomResourceDefinition\n",
      "NAME                                         AGE\n",
      "seldondeployments.machinelearning.seldon.io  0s\n",
      "\n",
      "==> v1/ClusterRole\n",
      "seldon-operator-manager-role  0s\n",
      "\n",
      "==> v1/ClusterRoleBinding\n",
      "NAME                                 AGE\n",
      "seldon-operator-manager-rolebinding  0s\n",
      "\n",
      "==> v1/Service\n",
      "NAME                                        TYPE       CLUSTER-IP      EXTERNAL-IP  PORT(S)  AGE\n",
      "seldon-operator-controller-manager-service  ClusterIP  10.106.183.171  <none>       443/TCP  0s\n",
      "\n",
      "==> v1/StatefulSet\n",
      "NAME                                DESIRED  CURRENT  AGE\n",
      "seldon-operator-controller-manager  1        1        0s\n",
      "\n",
      "==> v1/Pod(related)\n",
      "NAME                                  READY  STATUS             RESTARTS  AGE\n",
      "seldon-operator-controller-manager-0  0/1    ContainerCreating  0         0s\n",
      "\n",
      "\n",
      "NOTES:\n",
      "NOTES: TODO\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!helm install ../../helm-charts/seldon-core-operator --name seldon-core --set usageMetrics.enabled=true   --namespace seldon-system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "partitioned roll out complete: 1 new pods have been updated...\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl rollout status deploy/seldon-controller-manager -n seldon-system"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup Ingress\n",
    "Please note: There are reported gRPC issues with ambassador (see https://github.com/SeldonIO/seldon-core/issues/473)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error: a release named ambassador already exists.\r\n",
      "Run: helm ls --all ambassador; to check the status of the release\r\n",
      "Or run: helm del --purge ambassador; to delete it\r\n"
     ]
    }
   ],
   "source": [
    "!helm install stable/ambassador --name ambassador --set crds.keep=false"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "deployment \"ambassador\" successfully rolled out\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl rollout status deployment.apps/ambassador"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME:   seldon-gateway\n",
      "LAST DEPLOYED: Thu May  9 13:32:34 2019\n",
      "NAMESPACE: seldon\n",
      "STATUS: DEPLOYED\n",
      "\n",
      "RESOURCES:\n",
      "==> v1/ClusterRoleBinding\n",
      "NAME           AGE\n",
      "seldon         0s\n",
      "seldon-seldon  0s\n",
      "\n",
      "==> v1/Service\n",
      "NAME                             TYPE       CLUSTER-IP      EXTERNAL-IP  PORT(S)                        AGE\n",
      "seldon-gateway-redis-master      ClusterIP  10.105.144.238  <none>       6379/TCP                       0s\n",
      "seldon-gateway-seldon-apiserver  NodePort   10.111.157.247  <none>       8080:30110/TCP,5000:30172/TCP  0s\n",
      "\n",
      "==> v1beta1/Deployment\n",
      "NAME                             DESIRED  CURRENT  UP-TO-DATE  AVAILABLE  AGE\n",
      "seldon-gateway-seldon-apiserver  1        1        1           0          0s\n",
      "\n",
      "==> v1beta2/StatefulSet\n",
      "NAME                         DESIRED  CURRENT  AGE\n",
      "seldon-gateway-redis-master  1        1        0s\n",
      "\n",
      "==> v1/Pod(related)\n",
      "NAME                                              READY  STATUS             RESTARTS  AGE\n",
      "seldon-gateway-seldon-apiserver-85db7687b9-xzn7t  0/1    ContainerCreating  0         0s\n",
      "seldon-gateway-redis-master-0                     0/1    ContainerCreating  0         0s\n",
      "\n",
      "==> v1/ConfigMap\n",
      "NAME                         DATA  AGE\n",
      "seldon-gateway-redis         3     0s\n",
      "seldon-gateway-redis-health  3     0s\n",
      "\n",
      "==> v1/ServiceAccount\n",
      "NAME    SECRETS  AGE\n",
      "seldon  1        0s\n",
      "\n",
      "==> v1/ClusterRole\n",
      "NAME               AGE\n",
      "seldon-crd-seldon  0s\n",
      "seldon-wide        0s\n",
      "\n",
      "\n",
      "NOTES:\n",
      "Thank you for installing Seldon Core.\n",
      "\n",
      "Documentation can be found at https://github.com/SeldonIO/seldon-core\n",
      "\n",
      "\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "! helm install ../../helm-charts/seldon-core-oauth-gateway --name seldon-gateway --namespace seldon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "deployment \"seldon-gateway-seldon-apiserver\" successfully rolled out\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl rollout status deployment.apps/seldon-gateway-seldon-apiserver"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Set up REST and gRPC methods\n",
    "\n",
    "**Ensure you port forward ambassador and API gateway**:\n",
    "\n",
    "```\n",
    "kubectl port-forward $(kubectl get pods -n seldon -l app.kubernetes.io/name=ambassador -o jsonpath='{.items[0].metadata.name}') -n seldon 8003:8080\n",
    "```\n",
    "\n",
    "```\n",
    "kubectl port-forward $(kubectl get pods -n seldon -l app=seldon-apiserver-container-app -o jsonpath='{.items[0].metadata.name}') -n seldon 8002:8080\n",
    "```\n",
    "\n",
    "```\n",
    "kubectl port-forward $(kubectl get pods -n seldon -l app=seldon-apiserver-container-app -o jsonpath='{.items[0].metadata.name}') -n seldon 8004:5000\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Serve Single Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "namespace/test1 created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create namespace test1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME:   mymodel\r\n",
      "LAST DEPLOYED: Thu May  9 11:10:13 2019\r\n",
      "NAMESPACE: test1\r\n",
      "STATUS: DEPLOYED\r\n",
      "\r\n",
      "RESOURCES:\r\n",
      "==> v1alpha2/SeldonDeployment\r\n",
      "NAME     AGE\r\n",
      "mymodel  0s\r\n",
      "\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "!helm install ../../helm-charts/seldon-single-model --name mymodel --set oauth.key=oauth-key --set oauth.secret=oauth-secret --namespace test1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Waiting for deployment \"mymodel-mymodel-7cd068f\" rollout to finish: 0 of 1 updated replicas are available...\n",
      "deployment \"mymodel-mymodel-7cd068f\" successfully rolled out\n"
     ]
    }
   ],
   "source": [
    "!kubectl rollout status deploy/mymodel-mymodel-7cd068f -n test1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Get predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from seldon_core.seldon_client import SeldonClient\n",
    "sc = SeldonClient(deployment_name=\"mymodel\",namespace=\"test1\",oauth_key=\"oauth-key\",oauth_secret=\"oauth-secret\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### REST Request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.48380379505104043\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"1p875752kj5uh12b8h78ls3ueu\"\n",
      "  requestPath {\n",
      "    key: \"classifier\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.08070082081279971\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "p = sc.predict(gateway=\"seldon\",transport=\"rest\")\n",
    "print(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.4366478560418421\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"ecc6f4vqarkgjdr6lhvrn8kuh4\"\n",
      "  requestPath {\n",
      "    key: \"classifier\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.07727086092703452\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "p = sc.predict(gateway=\"ambassador\",transport=\"rest\")\n",
    "print(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### gRPC Request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.7613856135572777\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"t2ahkipoe6fpi7omipb50psler\"\n",
      "  requestPath {\n",
      "    key: \"classifier\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.10383875884578245\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "p = sc.predict(gateway=\"ambassador\",transport=\"grpc\")\n",
    "print(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.41050638142891616\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"8e81ftst2re3988t91t993tlg9\"\n",
      "  requestPath {\n",
      "    key: \"classifier\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.07542744792497805\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "p = sc.predict(gateway=\"seldon\",transport=\"grpc\")\n",
    "print(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "release \"mymodel\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!helm delete mymodel --purge"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Serve AB Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME:   myabtest\n",
      "LAST DEPLOYED: Thu May  9 14:10:03 2019\n",
      "NAMESPACE: test1\n",
      "STATUS: DEPLOYED\n",
      "\n",
      "RESOURCES:\n",
      "==> v1alpha2/SeldonDeployment\n",
      "NAME      AGE\n",
      "myabtest  1s\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!helm install ../../helm-charts/seldon-abtest --name myabtest --set oauth.key=oauth-key --set oauth.secret=oauth-secret --namespace test1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Waiting for deployment \"myabtest-abtest-41de5b8\" rollout to finish: 0 of 1 updated replicas are available...\n",
      "deployment \"myabtest-abtest-41de5b8\" successfully rolled out\n",
      "deployment \"myabtest-abtest-df66c5c\" successfully rolled out\n"
     ]
    }
   ],
   "source": [
    "!kubectl rollout status deploy/myabtest-abtest-41de5b8 -n test1\n",
    "!kubectl rollout status deploy/myabtest-abtest-df66c5c -n test1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Get predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "sc = SeldonClient(deployment_name=\"myabtest\",namespace=\"test1\",oauth_key=\"oauth-key\",oauth_secret=\"oauth-secret\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.7708073955451095\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"vssct0pmpvb8jn7m0tr5qqat2j\"\n",
      "  routing {\n",
      "    key: \"myabtest\"\n",
      "    value: 1\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"classifier-2\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"myabtest\"\n",
      "    value: \"\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.10471879300072064\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "r = sc.predict(gateway=\"seldon\",transport=\"rest\")\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.19907312279876477\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"vt20gbuqvmnp6ut7mtuni85mj9\"\n",
      "  routing {\n",
      "    key: \"myabtest\"\n",
      "    value: 0\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"classifier-1\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"myabtest\"\n",
      "    value: \"\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.06194314895751282\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "r = sc.predict(gateway=\"ambassador\",transport=\"rest\")\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### gRPC Request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.3612252639333452\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"jl72f5r3rkfgmntnu3d4bgf1e1\"\n",
      "  routing {\n",
      "    key: \"myabtest\"\n",
      "    value: 1\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"classifier-2\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"myabtest\"\n",
      "    value: \"\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.07206177651670163\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "r = sc.predict(gateway=\"ambassador\",transport=\"grpc\")\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.8232759661612369\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"9stsrlrtg26rgv495t0jv650d1\"\n",
      "  routing {\n",
      "    key: \"myabtest\"\n",
      "    value: 1\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"classifier-2\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"myabtest\"\n",
      "    value: \"\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.10974087173093611\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "r = sc.predict(gateway=\"seldon\",transport=\"grpc\")\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "release \"myabtest\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!helm delete myabtest --purge"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Serve Multi-Armed Bandit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME:   mymab\n",
      "LAST DEPLOYED: Thu May  9 14:10:59 2019\n",
      "NAMESPACE: test1\n",
      "STATUS: DEPLOYED\n",
      "\n",
      "RESOURCES:\n",
      "==> v1alpha2/SeldonDeployment\n",
      "NAME   AGE\n",
      "mymab  0s\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!helm install ../../helm-charts/seldon-mab --name mymab --set oauth.key=oauth-key --set oauth.secret=oauth-secret --namespace test1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Waiting for deployment \"mymab-abtest-41de5b8\" rollout to finish: 0 of 1 updated replicas are available...\n",
      "deployment \"mymab-abtest-41de5b8\" successfully rolled out\n",
      "deployment \"mymab-abtest-b8038b2\" successfully rolled out\n",
      "deployment \"mymab-abtest-df66c5c\" successfully rolled out\n"
     ]
    }
   ],
   "source": [
    "!kubectl rollout status deploy/mymab-abtest-41de5b8 -n test1\n",
    "!kubectl rollout status deploy/mymab-abtest-b8038b2 -n test1\n",
    "!kubectl rollout status deploy/mymab-abtest-df66c5c -n test1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Get predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "sc = SeldonClient(deployment_name=\"mymab\",namespace=\"test1\",oauth_key=\"oauth-key\",oauth_secret=\"oauth-secret\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.08889991742801762\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"mtmtjcp084o8rt32d7mf6ilcrj\"\n",
      "  routing {\n",
      "    key: \"eg-router\"\n",
      "    value: 0\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"classifier-1\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"eg-router\"\n",
      "    value: \"seldonio/mab_epsilon_greedy:1.1\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.05584202643308697\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "r = sc.predict(gateway=\"seldon\",transport=\"rest\")\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.8640733331929078\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"8t5bi7f1n5lcm9j3qa3qf6qg5l\"\n",
      "  routing {\n",
      "    key: \"eg-router\"\n",
      "    value: 0\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"classifier-1\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"eg-router\"\n",
      "    value: \"seldonio/mab_epsilon_greedy:1.1\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.11379060152228694\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "r = sc.predict(gateway=\"ambassador\",transport=\"rest\")\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### gRPC Request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.774689972995667\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"ir9slmf3ajkia1em6dcrje545a\"\n",
      "  routing {\n",
      "    key: \"eg-router\"\n",
      "    value: 1\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"classifier-2\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"eg-router\"\n",
      "    value: \"seldonio/mab_epsilon_greedy:1.1\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.10508335441938535\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "r = sc.predict(gateway=\"ambassador\",transport=\"grpc\")\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.4248889584516323\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"muacde1bh7fvpmq1pljf7huics\"\n",
      "  routing {\n",
      "    key: \"eg-router\"\n",
      "    value: 0\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"classifier-1\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"eg-router\"\n",
      "    value: \"seldonio/mab_epsilon_greedy:1.1\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.07643660719233908\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "r = sc.predict(gateway=\"seldon\",transport=\"grpc\")\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "release \"mymab\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!helm delete mymab --purge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
